package org.dromara.system.service.impl;

import org.dromara.system.domain.vo.CommissionImportVo;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StreamUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.CommissionBo;
import org.dromara.system.domain.vo.CommissionVo;
import org.dromara.system.domain.Commission;
import org.dromara.system.mapper.CommissionMapper;
import org.dromara.system.service.ICommissionService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 佣金信息Service业务层处理
 *
 * @author joker
 * @date 2025-05-05
 */
@RequiredArgsConstructor
@Service
public class CommissionServiceImpl implements ICommissionService {

    private final CommissionMapper baseMapper;

    /**
     * 查询佣金信息
     *
     * @param commissionId 主键
     * @return 佣金信息
     */
    @Override
    public CommissionVo queryById(Long commissionId){
        return baseMapper.selectVoById(commissionId);
    }

    /**
     * 分页查询佣金信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 佣金信息分页列表
     */
    @Override
    public TableDataInfo<CommissionVo> queryPageList(CommissionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Commission> lqw = buildQueryWrapper(bo);
        Page<CommissionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的佣金信息列表
     *
     * @param bo 查询条件
     * @return 佣金信息列表
     */
    @Override
    public List<CommissionVo> queryList(CommissionBo bo) {
        LambdaQueryWrapper<Commission> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Commission> buildQueryWrapper(CommissionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Commission> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(Commission::getCommissionId);
        lqw.like(StringUtils.isNotBlank(bo.getCommissionName()), Commission::getCommissionName, bo.getCommissionName());
        lqw.eq(StringUtils.isNotBlank(bo.getCommissionValidityPeriod()), Commission::getCommissionValidityPeriod, bo.getCommissionValidityPeriod());
        lqw.eq(StringUtils.isNotBlank(bo.getCommissionMoney()), Commission::getCommissionMoney, bo.getCommissionMoney());
        return lqw;
    }

    /**
     * 新增佣金信息
     *
     * @param bo 佣金信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(CommissionBo bo) {
        Commission add = MapstructUtils.convert(bo, Commission.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCommissionId(add.getCommissionId());
        }
        return flag;
    }

    @Override
    public Boolean insertList(List<CommissionImportVo> list) {
        List<Commission> commissions = StreamUtils.toList(list, commissionImportVo -> {
            Commission commission = new Commission();
            BeanUtils.copyProperties(commissionImportVo,commission);
            return commission;
        });
        return baseMapper.insertBatch(commissions);
    }

    /**
     * 修改佣金信息
     *
     * @param bo 佣金信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(CommissionBo bo) {
        Commission update = MapstructUtils.convert(bo, Commission.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Commission entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除佣金信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
